#include "glPrintf.h"

//#include <varargs.h>
#include <string.h>

#include "..\glut\glut.h"

#include "..\cuCommon\cuTypes.h"

#pragma warning(disable: 4996) // disable function || variable may be unsafe warning

int32 glPrintf(real32 x, real32 y, float4 * pColor, void * pFont, const char * pStr, ...)
{
  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
  glLoadIdentity();

  glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  glLoadIdentity();

  glColor4fv( (GLfloat *) pColor);
  glRasterPos2f(x, y);

  int32 strWidth = 0;

  if (pStr)
  {
    char Str[256];
    va_list	   ap;

    va_start(ap, pStr);					
    vsprintf(Str, pStr, ap);
    va_end(ap);

    size_t slen = strlen(Str);

    if (slen) 
    {
      for (uint32 is = 0; is < slen; is++)
      {
        glutBitmapCharacter(pFont, Str[is]);
        strWidth += glutBitmapWidth(pFont, Str[is]);
      }   
    }
  }

  glMatrixMode(GL_PROJECTION);
  glPopMatrix();  

  glMatrixMode(GL_MODELVIEW);
  glPopMatrix();

  return strWidth;
}